<?php
/**
 * Created by PhpStorm.
 * User: Jerry
 * Date: 2016/12/2
 * Time: 18:33
 */

namespace SDXApi\Controller;


use Think\Controller;

/**
 * Class SalesHandleController
 * @package SDXApi\Controller
 */
class SalesHandleController extends Controller
{
	/**
	 * @var \Model|\Think\Model
	 */
	protected $goodsModel;
	/**
	 * @var \Model|\Think\Model
	 */
	protected $operateLogModel;

	/**
	 * 参数预处理
	 * SalesHandleController constructor.
	 */
	public function __construct()
	{
		$this->goodsModel = D('Goods');
		$this->operateLogModel = D('OperateLog');
	}

	/**
	 * 脚本执行入口
	 * Created by Jerry
	 */
	public function handle()
	{
//		先清除所有促销类型的商品数据
		$this->delete_operate_by_sales_type();

//		插入所有促销类型商品数据
		$this->check_sales_handle();

//		插入所有秒杀商品数据
		$this->check_sales_buy_now_handle();
	}

	/**
	 * 获取促销商品并插入
	 * Created by Jerry
	 */
	protected function check_sales_handle()
	{
		$data = $this->get_sales_goods();

		return $this->insert_sales_goods_for_operate($data);
	}

	/**
	 * 获取秒杀商品并插入
	 * Created by Jerry
	 * @return bool
	 */
	protected function check_sales_buy_now_handle()
	{
		$data = $this->get_buy_now_goods();

		if (empty($data)) {
			return false;
		}

		return $this->insert_sales_goods_buy_now_for_operate($data);
	}



	/**
	 * 获取促销商品信息
	 * Created by Jerry
	 * @return mixed
	 */
	protected function get_sales_goods()
	{
		return $this->goodsModel->get_sales_goods_by_promote_price_for_list();
	}

	/**
	 * 返回秒杀商品
	 * Created by Jerry
	 * @return mixed
	 */
	protected function get_buy_now_goods()
	{
		return $this->goodsModel->get_sales_goods_by_is_buynow_for_list();
	}

	/**
	 * 插入促销商品信息
	 * Created by Jerry
	 * @param $data
	 * @return bool
	 */
	protected function insert_sales_goods_for_operate($data)
	{
		if (empty($data)) {
			return false;
		}

		foreach ($data as $key => $goods) {
			$operate = $this->check_sales_date_for_goods($goods);
			$this->insert_sales_goods_operate($operate);
		}

		return true;
	}

	/**
	 * 插入秒杀商品信息
	 * Created by Jerry
	 * @param $data
	 * @return bool
	 */
	protected function insert_sales_goods_buy_now_for_operate($data)
	{
		if (empty($data)) {
			return false;
		}

		foreach ($data as $key => $goods) {
			$operate = $this->check_sales_date_buy_now_for_goods($goods);
			$this->insert_sales_goods_operate($operate);
		}

		return true;
	}

	/**
	 * 插入预警日志
	 * Created by Jerry
	 * @param $operate
	 * @return mixed
	 */
	protected function insert_sales_goods_operate($operate)
	{
		return $this->operateLogModel->insert_sales_goods_for_operate($operate);
	}

	/**
	 * 处理促销商品类型信息
	 * Created by Jerry
	 * @param $goods
	 * @return bool
	 */
	protected function check_sales_date_for_goods($goods)
	{
		if (empty($goods)) {
			return false;
		}

//		判断促销价是否已过期
		if ($goods['promote_start_date'] < gmtime() && $goods['promote_end_date'] < gmtime()) {
			$goods['type'] = 4;
		}

//		判断促销价是否正在进行中
		if ($goods['promote_start_date'] < gmtime() && $goods['promote_end_date'] > gmtime()) {
			$goods['type'] = 5;
		}

//		判断促销价是否未开始
		if ($goods['promote_start_date'] > gmtime() && $goods['promote_end_date'] > gmtime()) {
			$goods['type'] = 6;
		}

		return $goods;
	}

	/**
	 * 处理促销商品类型信息
	 * Created by Jerry
	 * @param $goods
	 * @return bool
	 */
	protected function check_sales_date_buy_now_for_goods($goods)
	{
		if (empty($goods)) {
			return false;
		}

//		判断秒杀价是否已过期
		if ($goods['promote_start_date'] < gmtime() && $goods['promote_end_date'] < gmtime()) {
			$goods['type'] = 13;
		}

//		判断秒杀价是否正在进行中
		if ($goods['promote_start_date'] < gmtime() && $goods['promote_end_date'] > gmtime()) {
			$goods['type'] = 14;
		}

//		判断秒杀价是否未开始
		if ($goods['promote_start_date'] > gmtime() && $goods['promote_end_date'] > gmtime()) {
			$goods['type'] = 15;
		}

		return $goods;
	}

	/**
	 * 删除所有促销类型数据
	 * Created by Jerry
	 */
	protected function delete_operate_by_sales_type()
	{
		$where = array(
			'type'	=>	array('IN', '4, 5, 6, 13, 14, 15'),
		);

		$this->operateLogModel->detele_operate_by_sales_type($where);
	}
}